﻿using System;
using Assets.AnimationPlayer.Scripts.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Assets.AnimationPlayer.Scripts.Util
{
    public class ArApi : MonoBehaviour
    {
        public bool ShouldReset = true;//
        public bool AutoPlayModel = true;//自动播放模式，手动播放模式
        internal void Awake()
        {
            name = "ARAPI";
            AppDomain.CurrentDomain.UnhandledException += _unHandledException;
        }

        private void _unHandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //AniamtionVar.ResetVar();
            //AniamtionVar.BackToList();
            AnimationVar.AnimationPlayCfg.AutoPlay = true;
            AnimationVar.AnimationPlayCfg.SelectAnimationIndex = -1;
            //AniamtionVar.ResetVar();
            //AniamtionVar.BackToList();
            API_ReStart();
        }

        #region API
        #region Listener

        public void UNITY_set_animation(string animationName)
        {
            if (string.IsNullOrEmpty(animationName))
            {
                if(SceneManager.GetActiveScene() != null && SceneManager.GetActiveScene().name != "AnimationListView")
                {
                    ShouldReset = true;
                    AnimationVar.BackToList();
                }
                return;
            }
            Debug.Log("=============="+animationName +"**********************"+ AnimationVar.AnimationPlayCfg.SelectAnimationObj.AnimationName);
            if (ShouldReset)
            {

            }
            else
            {
                if (AnimationVar.AnimationPlayCfg.SelectAnimationObj.AnimationName.Equals(animationName)) return;
            }

            int index =
                AnimationVar.AnimationPlayCfg.Cfg.AnimationObjs.FindIndex(t => t.AnimationName.Equals(animationName));
            Debug.Log("==============" + index);
            if (index != -1)
            {
                ShouldReset = false;
                AnimationVar.AnimationPlayCfg.SelectAnimationIndex = index;
                AnimationVar.PlayAnimation();
  
            }

        }
        #endregion

        #region Callback
        public static bool ApiInit;
        public void API_LoadingComplete()
        {
            if (ApiInit) return;
            ApiInit = true;
            //UNITY_set_animation("颠球");
            //Debug.Log("complete_load*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("complete_load");
                        }
                    }
#endif
                }
            }
            catch
            {
                //
            }
        }

        public void API_ReStart()
        {
            //Debug.Log("unity_restart*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("unity_restart");
                        }
                    }
#endif
                }
                else
                {
                    AnimationVar.BackToList();
                }
            }
            catch
            {
                //
            }
        }
        public void API_Break()
        {
            //Debug.Log("unity_finish*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("unity_finish");
                        }
                    }
                    Application.Quit();
#endif
                }
                else
                {
                    AnimationVar.BackToList();
                }
            }
            catch
            {
                //
            }
        }
        public void API_Update()
        {
            //Debug.Log("unity_update*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {

                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("unity_update");
                        }
                    }
#endif
                }
                else
                {
                    AnimationVar.BackToList();
                }
            }
            catch
            {
                //
            }
        }
        public void API_Setting()
        {
            //Debug.Log("unity_setting*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("unity_setting");
                        }
                    }
#endif
                }
                else
                {
                    AnimationVar.BackToList();
                }
            }
            catch
            {
                //
            }
        }
        public void API_Uninstall()
        {
            //Debug.Log("unity_uninstall*****>device");
            try
            {
                if (Application.platform == RuntimePlatform.IPhonePlayer)
                {
                }
                else if (Application.platform == RuntimePlatform.Android)
                {
#if UNITY_ANDROID
                    using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                    {
                        using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                        {
                            jo.Call("unity_uninstall");
                        }
                    }
#endif
                }
                else
                {
                    AnimationVar.BackToList();
                }
            }
            catch
            {
                //
            }
        }
        #endregion

        #endregion



    }
}
